package de.gameduell.framework.gui.style {
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.util.HashMap;
	
	import flash.geom.Point;
	
	/**
	 * convert xml data into button states and add textfields
	 */
	public class StyleConfig {
		
		protected static var instance:StyleConfig;
		private var styleMap:HashMap;
		
		public function StyleConfig() {
			styleMap = new HashMap();
		}
		
		public static function getInstance():StyleConfig {
			if (instance == null) {
				instance = new StyleConfig();
			}
			
			return instance;
		}
		
		public function addXML(aXML:XML):void{
			var nodes:XMLList = aXML.children();
			for(var i:* in nodes){
				var node:XML = XML(nodes[i]);
				styleMap.put(node.name().toString(), parseStyleDescriptor(node));
				Debug.trace("StyleConfig add " + node.name());
			}
		}
		
		private function parseStyleDescriptor(node:XML):StyleDescriptor{
			var descriptor:StyleDescriptor = new StyleDescriptor();
			var nodes:XMLList = node.children();

			// fallback values (defined in button for all states)
			var fallbackTextEnabled:Boolean 	= node.attribute("textEnabled") == "true";	
			var fallbackTextWidth:Number 		= parseAttributeAsNumber(node, "textWidth");
			var fallbackTextHeight:Number 		= parseAttributeAsNumber(node, "textHeight");
			var fallbackTextX:Number = parseAttributeAsNumber(node, "textX");
			var fallbackTextY:Number = parseAttributeAsNumber(node, "textY");
			
			for(var i:* in nodes){
				var stateNode:XML = XML(nodes[i]);
				var stateType:String = stateNode.name().toString();
				var libraryId:String = stateNode.text().toString();
				
				var text:Boolean 	 = hasAttribute(stateNode, "textEnabled") ? parseAttributeAsBoolean(stateNode, "textEnabled") : fallbackTextEnabled;
				var width:Number 	 = hasAttribute(stateNode, "textWidth") ? parseAttributeAsNumber(stateNode, "textWidth") : fallbackTextWidth;
				var height:Number 	 = hasAttribute(stateNode, "textHeight") ? parseAttributeAsNumber(stateNode, "textHeight") : fallbackTextHeight;
				var x:Number = hasAttribute(stateNode, "textX") ? parseAttributeAsNumber(stateNode, "textX") : fallbackTextX;
				var y:Number = hasAttribute(stateNode, "textY") ? parseAttributeAsNumber(stateNode, "textY") : fallbackTextY;
				
				descriptor.registerState(stateType,	libraryId, text, new Point(x, y), new Point(width, height));
			} 
			
			return descriptor;
		}
		
		private function hasAttribute(node:XML, attributeKey:String):Boolean {
			var result:XMLList = node.attribute(attributeKey);
			return result.length() > 0;
		}

		private function parseAttributeAsBoolean(node:XML, attributeKey:String):Boolean {
			var result:XMLList = node.attribute(attributeKey);
			return node.attribute(attributeKey) == "true";
		}
		
		private function parseAttributeAsString(node:XML, attributeKey:String):String {
			var result:XMLList = node.attribute(attributeKey);
			return result.length() > 0 ? result.text() : null;
		}

		private function parseAttributeAsNumber(node:XML, attributeKey:String):Number {
			var result:XMLList = node.attribute(attributeKey);
			return result.length() > 0 ? new Number(result) : 0;
		}
		
		public function getStyleDescriptor(styleId:String):StyleDescriptor {
			Debug.trace("StyleConfig size="+styleMap.length()+" keys="+styleMap.keys+" foundKey=" + (styleMap.keys.indexOf(styleId) != -1)+ " getStyleDescriptor " + styleId + " -> " + styleMap.getValue(styleId));
 			return StyleDescriptor(styleMap.getValue(styleId));
		}

	}
}